/* 
 * File:   prueba2.cpp
 * Author: saulio
 * Created on April 18, 2012, 3:12 PM
 */

#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#include "Frame.h"

using namespace std;



int main() {
    
     //static const int QUEUE=5;
     char buff[200];
     unsigned short Quimera_port=3000;
     struct msghdr mh;
     struct iovec v[1];
     struct sockaddr_in serv_addr, cli_addr;
     int sockfd, r_n_bytes;
     bzero((char *) &serv_addr, sizeof(serv_addr));
     bzero((char *) &cli_addr, sizeof(cli_addr));
     bzero((char *) &buff, sizeof(buff));
     bzero((char *) &mh, sizeof(mh));
     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
     v[0].iov_base=&buff;
     v[0].iov_len=sizeof(buff);
    // v[1].iov_base=&r;
     //v[1].iov_len=sizeof(r);
     
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_family=AF_INET;
     serv_addr.sin_port = htons(707);
     bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr));
    // listen(sockfd,QUEUE);
     //clilen = sizeof(cli_addr);
     //newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    mh.msg_name =  &cli_addr;
    mh.msg_namelen = sizeof(cli_addr);
    mh.msg_iov = v;
    mh.msg_iovlen = 1;
    
    cout << "aqui saleta" << endl;
    r_n_bytes=recvmsg(sockfd, &mh, 0);

    cout << buff << endl;
   /*      cout << r_n_bytes << endl;
              cout << "Se ha procesado un mensaje de investidura:" << endl; 
              cout << "La version es: " << int(header.version) << endl;
              cout << "La tipo de mensaje es: " <<int(header.type_message) << endl;
              cout << "El dominio es: " << int(header.domain) << endl;
              cout << "El id del servidor fuente es: " << header.id_server_source << endl;
              cout << "El id del servidor destino es: " << header.id_server_destination << endl;
              cout << "El flag esta marcado con: " << int(header.flag) << endl;
              cout << "El offset es " << header.offset << endl;
              cout << "Se ha procesado un mensaje de investidura:" << endl; 
              cout << "aknowledge " << r.akcnowledge_number << endl;
              cout << "Secuencia " << r.sequence_number << endl;
     */                
    
     close(sockfd);
     return 0;
    
     
}

/*void Header::Print_header(Header &tmp)
{
    cout << "La version es: " << int(tmp.header.version) << endl;
    cout << "La tipo de mensaje es: " <<int(tmp.header.type_message) << endl;
    cout << "El dominio es: " << int(tmp.header.domain) << endl;
    cout << "El id del servidor fuente es: " << tmp.header.id_server_source << endl;
    cout << "El id del servidor destino es: " << tmp.header.id_server_destination << endl;
    cout << "El flag esta marcado con: " << int(tmp.header.flag) << endl;
    cout << "El offset es " << tmp.header.offset << endl;    


 cout << "Se ha procesado un mensaje de investidura:" << endl; 
              cout << "La version es: " << int(header.version) << endl;
              cout << "La tipo de mensaje es: " <<int(header.type_message) << endl;
              cout << "El dominio es: " << int(header.domain) << endl;
              cout << "El id del servidor fuente es: " << header.id_server_source << endl;
              cout << "El id del servidor destino es: " << header.id_server_destination << endl;
              cout << "El flag esta marcado con: " << int(header.flag) << endl;
              cout << "El offset es " << header.offset << endl;
              cout << "Se ha procesado un mensaje de investidura:" << endl; 
              cout << "aknowledge " << package.guard.akcnowledge_number << endl;
              cout << "Secuencia " << package.guard.sequence_number << endl;

}*/
